﻿using H.Utils.DataStruct;
using HMaker.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HMaker.Dialogs
{
    public partial class Magnifier : Form
    {
        /// <summary>
        /// 当前背景图片
        /// </summary>
        private Image _CurImage;

        /// <summary>
        /// 存储当前的点在背景上的坐标
        /// </summary>
        private Point _CurPoint;

        /// <summary>
        /// 存储当前的基准点在背景上的坐标
        /// </summary>
        private Point _BaseRelativePos;

        /// <summary>
        /// 存储缩放系数
        /// </summary>
        private int _Scalar = 5;

        /// <summary>
        /// 缩放系数
        /// </summary>
        public int Scalar
        {
            get
            {
                return _Scalar;
            }
            set
            {
                if (value % 2 == 1 && value >= 5 && value <= 33)
                {
                    _Scalar = value;
                    NUD_Scalar.Value = value;
                    PB_Main.Invalidate();
                }
            }
        }


        /// <summary>
        /// 用于画瞄准线的画笔
        /// </summary>
        private readonly Pen _LinePen = new Pen(Color.FromArgb(120, 0, 0, 0)) { DashStyle = DashStyle.Dash };

        /// <summary>
        /// 父窗体
        /// </summary>
        private readonly Form _ParentForm;


        public Magnifier()
        {
            InitializeComponent();
        }

        public Magnifier(Form maker) : this()
        {
            _ParentForm = maker;
            _ParentForm.LocationChanged += _ParentForm_LocationChanged;
            Activated += Magnifier_Activated;
            _ParentForm?.Focus();
        }

        private void Magnifier_Activated(object sender, EventArgs e)
        {
            _ParentForm?.Focus();
        }

        private void _ParentForm_LocationChanged(object sender, EventArgs e)
        {
            Location = _ParentForm.Location.Sub(_Offset);
        }

        public void SetImage(Image image)
        {
            _CurImage = image?.Clone() as Bitmap;
        }

        public void SetCurPos(Point point)
        {
            _CurPoint = point;
            TB_OriginPoint.Text = point.MakeString();
            UpdateBaseOffset();
            PB_Main.Invalidate();
        }

        public void SetBasePos(Point point)
        {
            _BaseRelativePos = point;
            TB_BaseRelativePos.Text = point.MakeString();
        }

        public void SetSize(Size size)
        {
            TB_Size.Text = size.MakeString();
        }

        public void UpdateBaseOffset()
        {
            TB_BaseOffset.Text = _CurPoint.Sub(_BaseRelativePos).MakeString();
        }

        private void Magnifier_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        Bitmap _Bitmap = new Bitmap(201, 201);

        private void PB_Main_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.None;
            e.Graphics.Clear(Color.White);

            if (_CurImage != null)
            {
                BitmapUtils.Enlarge((Bitmap)_CurImage, _Bitmap, _CurPoint, Color.FromArgb(255, 255, 0, 255), _Scalar);
                e.Graphics.DrawImage(_Bitmap, 0, 0);
            }
            e.Graphics.DrawLine(_LinePen, 0, 100, 200, 100);
            e.Graphics.DrawLine(_LinePen, 100, 0, 100, 200);
        }

        private void NUD_Scalar_ValueChanged(object sender, EventArgs e)
        {
            Scalar = (int)NUD_Scalar.Value;
        }

        private Point _Offset = Point.Empty;

        private void Magnifier_LocationChanged(object sender, EventArgs e)
        {
            if (_ParentForm != null)
            {
                _Offset = _ParentForm.Location.Sub(Location);
            }
        }
    }
}
